<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>物流信息管理系统</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.1.3/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            min-height: 100vh;
            background-color: #f8f9fa;
        }
        .navbar {
            position: fixed;
            top: 0;
            right: 0;
            left: 0;
            z-index: 1030;
            height: 60px;
            background-color: #fff;
            box-shadow: 0 2px 4px rgba(0,0,0,.1);
        }
        .sidebar {
            position: fixed;
            top: 60px;
            bottom: 0;
            left: 0;
            z-index: 1020;
            width: 240px;
            background-color: #fff;
            box-shadow: 1px 0 4px rgba(0,0,0,.1);
            overflow-y: auto;
        }
        .sidebar-sticky {
            padding-top: 1rem;
        }
        .main-content {
            margin-left: 240px;
            padding: 80px 20px 20px;
            min-height: calc(100vh - 60px);
        }
        .nav-link {
            color: #333;
            padding: 10px 20px;
            display: block;
            border-radius: 4px;
            margin: 4px 8px;
        }
        .nav-link:hover {
            background-color: #f8f9fa;
            text-decoration: none;
        }
        .nav-link.active {
            background-color: #0d6efd;
            color: white;
        }
        .nav-link i {
            margin-right: 10px;
            width: 20px;
            text-align: center;
        }
        .card {
            margin-bottom: 20px;
            border: none;
            box-shadow: 0 0 10px rgba(0,0,0,.05);
            border-radius: 8px;
        }
        .stat-card {
            text-align: center;
            padding: 20px;
            background: linear-gradient(45deg, #fff, #f8f9fa);
        }
        .stat-card i {
            font-size: 2rem;
            margin-bottom: 10px;
        }
        .stat-card .number {
            font-size: 1.5rem;
            font-weight: bold;
            margin: 10px 0;
        }
        .stat-card .label {
            color: #6c757d;
            font-size: 0.9rem;
        }
        .card-title {
            color: #333;
            font-weight: 600;
            margin-bottom: 1rem;
        }
        .chart-container {
            height: 300px;
            padding: 10px;
        }
    </style>
</head>
<body>
    <!-- 顶部导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-light">
        <div class="container-fluid">
            <a class="navbar-brand" href="/">物流信息管理系统</a>
            <div class="d-flex align-items-center">
                <span class="navbar-text me-3" id="userInfo">
                    欢迎，<span id="username"></span>
                </span>
                <button class="btn btn-outline-danger btn-sm" onclick="logout()">退出登录</button>
            </div>
        </div>
    </nav>

    <!-- 侧边栏 -->
    <nav class="sidebar">
        <div class="sidebar-sticky">
            <ul class="nav flex-column">
                <li class="nav-item">
                    <a class="nav-link active" href="/dashboard">
                        <i class="fas fa-tachometer-alt"></i>
                        仪表盘
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/order">
                        <i class="fas fa-box"></i>
                        订单管理
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/transport">
                        <i class="fas fa-truck"></i>
                        运输管理
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/warehouse">
                        <i class="fas fa-warehouse"></i>
                        仓库管理
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/storage">
                        <i class="fas fa-database"></i>
                        存储记录
                    </a>
                </li>
            </ul>
        </div>
    </nav>

    <!-- 主要内容区域 -->
    <main class="main-content">
        <div class="container-fluid">
            <!-- 统计卡片 -->
            <div class="row g-4">
                <div class="col-md-3">
                    <div class="card stat-card">
                        <i class="fas fa-box text-primary"></i>
                        <div class="number" id="pendingOrders">0</div>
                        <div class="label">待处理订单</div>
                    </div>
                        </div>
                <div class="col-md-3">
                    <div class="card stat-card">
                        <i class="fas fa-truck text-success"></i>
                        <div class="number" id="inTransit">0</div>
                        <div class="label">运输中</div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card stat-card">
                        <i class="fas fa-warehouse text-warning"></i>
                        <div class="number" id="warehouseUsage">0%</div>
                        <div class="label">仓库使用率</div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card stat-card">
                        <i class="fas fa-chart-line text-info"></i>
                        <div class="number" id="totalOrders">0</div>
                        <div class="label">总订单数</div>
                    </div>
                </div>
            </div>
            
            <!-- 图表区域 -->
            <div class="row g-4 mt-2">
                <div class="col-md-6">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">订单状态分布</h5>
                            <div id="orderStatusChart" class="chart-container"></div>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">仓库使用率</h5>
                            <div id="warehouseUsageChart" class="chart-container"></div>
                        </div>
                    </div>
                </div>
                            </div>

            <div class="row mt-4">
                <div class="col-md-12">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">近7天订单趋势</h5>
                            <div id="orderTrendChart" class="chart-container"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.1.3/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.4.3/echarts.min.js"></script>
    <script>
        $(function() {
            // 获取用户信息
            $.get('/api/user/info', function(res) {
                if (res.code === 200) {
                    $('#username').text(res.data.username);
                } else {
                    window.location.href = '/login';
                }
            });

            // 获取统计数据
            loadStatistics();
            // 初始化图表
            initCharts();
        });

        function loadStatistics() {
            $.get('/api/dashboard/statistics', function(res) {
                if (res.code === 200) {
                    $('#pendingOrders').text(res.data.pendingOrders);
                    $('#inTransit').text(res.data.inTransit);
                    $('#warehouseUsage').text(res.data.warehouseUsage + '%');
                    $('#totalOrders').text(res.data.totalOrders);
                }
            });
        }

        function initCharts() {
            // 初始化订单状态分布图表
            var orderStatusChart = echarts.init(document.getElementById('orderStatusChart'));
            $.get('/api/dashboard/order-status', function(res) {
                if (res.code === 200) {
                    var option = {
                        tooltip: {
                            trigger: 'item'
                        },
                        legend: {
                            orient: 'vertical',
                            left: 'left'
                        },
                        series: [{
                            type: 'pie',
                            radius: '50%',
                            data: res.data,
                            emphasis: {
                                itemStyle: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }]
                    };
                    orderStatusChart.setOption(option);
                }
            });

            // 初始化仓库使用率图表
            var warehouseUsageChart = echarts.init(document.getElementById('warehouseUsageChart'));
            $.get('/api/dashboard/warehouse-usage', function(res) {
                if (res.code === 200) {
                    var option = {
                        tooltip: {
                            trigger: 'axis',
                            axisPointer: {
                                type: 'shadow'
                            }
                        },
                        xAxis: {
                            type: 'category',
                            data: res.data.map(item => item.name)
                        },
                        yAxis: {
                            type: 'value',
                            max: 100,
                            axisLabel: {
                                formatter: '{value}%'
                            }
                        },
                        series: [{
                            data: res.data.map(item => item.usage),
                            type: 'bar'
                        }]
                    };
                    warehouseUsageChart.setOption(option);
                }
            });

            // 初始化订单趋势图表
            var orderTrendChart = echarts.init(document.getElementById('orderTrendChart'));
            $.get('/api/dashboard/order-trend', function(res) {
                if (res.code === 200) {
                    var option = {
                        tooltip: {
                            trigger: 'axis'
                        },
                        xAxis: {
                            type: 'category',
                            data: res.data.map(item => item.date)
                        },
                        yAxis: {
                            type: 'value'
                        },
                        series: [{
                            data: res.data.map(item => item.count),
                            type: 'line',
                            smooth: true
                        }]
                    };
                    orderTrendChart.setOption(option);
                }
            });
        }

        function logout() {
            $.post('/api/user/logout', function() {
                window.location.href = '/login';
            });
        }

        // 窗口大小改变时重绘图表
        window.onresize = function() {
            var charts = ['orderStatusChart', 'warehouseUsageChart', 'orderTrendChart'];
            charts.forEach(function(id) {
                var chart = echarts.getInstanceByDom(document.getElementById(id));
                if (chart) {
                    chart.resize();
                }
            });
        };
    </script>
</body>
</html> 